/************************************************

*												*

*	Do-File which generates the main data sets	*

*												*

************************************************/

version 12.1
clear
clear matrix
set more off
capture log close


// Set working directory (has to be adjusted by the user)
cd "`c(pwd)'/"



*********************************************
*											*
*	(1) Insheet and prepare WIOD SEA data	* 	
*											*
*********************************************

	clear

	insheet using "flatfile_SEA.txt", names

	drop if code == ""

	sort country var code

	reshape long _, i(country var code) j(year)

	reshape wide _, i(country code year) j(var) string

	renpfix _

	compress

	foreach x of varlist CAP COMP EMP EMPE GFCF GFCF_P GO GO_P GO_QI H_EMP H_EMPE II II_P II_QI K_GFCF LAB VA VA_P VA_QI{
		
		replace `x' = "" if `x' == "NA"
		replace `x' = "" if `x' == ""
		replace `x' = "" if `x' == "Fehler 2015"
		destring `x', replace

	}


	* Generation of empty observations for sectors which are not available in WIOD but will be completed with other data sources
	foreach y in  AUS AUT BEL CZE DEU DNK ESP FIN FRA GBR HUN IRL ITA JPN NLD PRT SVN SWE USA {

		foreach z of numlist 1995 / 2009 {
		
			set obs `=_N+1'
			replace country = "`y'" if country == ""
			replace year = `z' if year == .
			replace code = "40x" if code == ""	
		}

	}

	rename code industry

	save "SEA.dta", replace




*********************************************
*											*
*	(2) Insheet and prepare GGDC PPP data	* 	
*											*
*********************************************

	clear

	* Generation of dta-template by using a first variable (PPP_CAP)
	xmluse "PPP_CAP.xml", doctype(excel) firstrow

	foreach var of varlist _all{
		rename `var' `=lower("`var'")'
	}
	rename euk industry

	drop ea eu25 eaex eu25ex eu15ex	eu15 eu_klems_industries
	keep if industry == "15t16" | industry == "17t19" | industry == "20" | industry == "21t22" | industry == "23" | industry == "23t25" | industry == "24" | industry == "25" | industry == "26" | industry == "27t28" | industry == "29" | ///
	industry == "30t33" | industry == "34t35" | industry == "36t37" | industry == "50" | industry == "51" | industry == "52" | industry == "60t63" | industry == "64" | industry == "70" | industry == "71t74" | industry == "AtB" | industry == "C" | ///
	industry == "D" | industry == "E" | industry == "F" | industry == "G" |  industry == "H" | industry == "I" | industry == "J" | industry == "JtK" | industry == "K" | industry == "L" | industry == "LtQ" | industry == "M" | industry == "N" | ///
	industry == "O" | industry == "P" | industry == "Q" | industry == "TOT"


	destring _all, replace
	tostring industry, replace
	foreach x of varlist _all {
		rename `x' ppp_cap_`x'	
	}
	rename ppp_cap_industry industry

	reshape long ppp_cap_, i(industry) j(country) string

	rename ppp_cap_ ppp_cap
	compress
	sort country industry 
	order country industry

	save "PPP97.dta", emptyok replace

	clear

	* Using the generated template to add the remaining variables
	foreach a in PPP_SO PPP_II PPP_VA PPP_LAB PPP_CAP {

		global VAR "`a'" 
		xmluse "${VAR}.xml", doctype(excel) firstrow
		
		foreach var of varlist _all{
			rename `var' `=lower("`var'")'
		}
		rename euk industry

		drop ea eu25 eaex eu25ex eu15ex	eu15 eu_klems_industries
		keep if industry == "15t16" | industry == "17t19" | industry == "20" | industry == "21t22" | industry == "23" | industry == "23t25" | industry == "24" | industry == "25" | industry == "26" | industry == "27t28" | industry == "29" | ///
		industry == "30t33" | industry == "34t35" | industry == "36t37" | industry == "50" | industry == "51" | industry == "52" | industry == "60t63" | industry == "64" | industry == "70" | industry == "71t74" | industry == "AtB" | industry == "C" | ///
		industry == "D" | industry == "E" | industry == "F" | industry == "G" |  industry == "H" | industry == "I" | industry == "J" | industry == "JtK" | industry == "K" | industry == "L" | industry == "LtQ" | industry == "M" | industry == "N" | ///
		industry == "O" | industry == "P" | industry == "Q" | industry == "TOT"


		destring _all, replace
		tostring industry, replace
		foreach x of varlist _all {
			rename `x' ${VAR}_`x'	
		}
		rename ${VAR}_industry industry

		reshape long ${VAR}_, i(industry) j(country) string

		rename `a'_ `=lower("`a'")' 
		compress
		sort country industry 
		order country industry

		merge country industry using "PPP97.dta", update
		sort country industry
		drop _merge 
		save "PPP97.dta", emptyok replace

		clear
	}

	use "PPP97.dta"

	compress

	replace country = upper(country)
	replace country = "DEU" if country == "GER"
	replace country = "JPN" if country == "JAP"
	replace country = "GBR" if country == "UK"


	destring country, replace
	sort country industry
	save "PPP97.dta", replace

	clear




*********************************************
*											*
*	(3) Insheet and prepare WIOD EM data	* 	
*											*
*********************************************

	clear

	insheet using "flatfile_EM_may12.txt", names

	drop if code == ""
	drop if code == "Q"

	replace code = substr(code, 4, .)

	drop if code == "al" | code == "HH" 											// Drop observations for "Final consumption expenditure by households" and for "Grand Total"

	sort country code year

	compress

	foreach y in AUS AUT BEL CZE DEU DNK ESP FIN FRA GBR HUN IRL ITA JPN NLD PRT SVN SWE USA {


		foreach z of numlist 1995 / 2007 {
		
			set obs `=_N+1'
			replace country = "`y'" if country == ""
			replace year = `z' if year == .
			replace code = "40x" if code == ""	
		
		}

	}


	rename code industry

	foreach a in  hcoal bcoal coke crude diesel gasoline jetfuel lfo hfo naphta othpetro natgas othgas waste biogasol biodiesel biogas othrenew electr heatprod nuclear hydro geotherm solar wind othsourc loss total{

		rename `a' EM_`a'
		label variable EM_`a' "Emission Relevant Energy Use (TJ) `a'" 

	}

	sort country industry year
	save "EM.dta", replace




*****************************************************************
*																*
*	(4) Insheet and prepare IEA Electricity Generation data		* 	
*																*
*****************************************************************

/*
Generated electricity by generation technology (IEA)
*/


	clear

	xmluse "IEA_elec_gen.xml", firstrow

	compress

	gen industry = "40x"

	rename Country country 
	rename Time year
	rename Nuclear EG_nuclear
	label variable EG_nuclear "Net electricity production (GWh) | total plants | nuclear"
	rename Hydro EG_hydro
	label variable EG_hydro "Net electricity production (GWh) | total plants | hydro"
	rename Pumped_hydro_Production EG_hydro_pump
	label variable EG_hydro_pump "Net electricity production (GWh) | total plants | pumped hydro"
	rename Geothermal EG_geotherm
	label variable EG_geotherm "Net electricity production (GWh) | total plants | geothermal"
	rename Solar EG_solar
	label variable EG_solar "Net electricity production (GWh) | total plants | solar"
	rename Solar_photovoltaic EG_solar_pv
	label variable EG_solar_pv "Net electricity production (GWh) | total plants | solar photovoltaic"
	rename Solar_thermal EG_solar_therm
	label variable EG_solar_therm "Net electricity production (GWh) | total plants | solar thermal"
	rename var10 EG_tidewaveocean
	label variable EG_tidewaveocean "Net electricity production (GWh) | total plants | tide, wave and ocean"
	rename Wind EG_wind
	label variable EG_wind "Net electricity production (GWh) | total plants | wind"
	rename Total_combustible_fuels EG_totcombust
	label variable EG_totcombust "Net electricity production (GWh) | total plants | total combustible"
	rename Total_sources EG_total
	label variable EG_total "Net electricity production (GWh) | total plants | total"

	replace country = "FRA" if country == "France"

	sort country industry year
	save "IEA_elec_gen40x.dta", replace


	

*****************************************************************
*																*
*	(5) Insheet and prepare IEA Electricity Capacity data		* 	
*																*
*****************************************************************

/*
Net installed electricity generation capacity (IEA)
*/

	clear

	xmluse "IEA_elec_cap.xml", firstrow

	compress

	gen industry = "40x"

	rename Country country
	rename Time year
	rename Total_capacity total
	rename Nuclear nuclear
	rename Hydro hydro
	rename Pumped_hydro_production hydro_pump
	rename Geothermal geotherm
	rename Solar solar
	rename Wind wind
	rename Combustible_fuels totcombust
	rename Other_fuel_sources other
	rename Coal_and_coal_products coal
	rename Liquid_fuels_and_refinery_gas lr_gas
	rename Natural_gas nat_gas 
	rename Peat peat
	rename Other_combustible_fuels other_combust
	rename Steam steam
	rename Gas_Turbine gas_turbine 
	rename Combined_cycle combined_cycle 
	rename Other_type_of_generation othertype
	rename Peak_load peakload
	rename Capacity_at_peak capatpeak
	rename var9 tidewaveocean
	rename var18 solid_liquid
	rename var19 solid_gas
	rename var20 liquid_gas
	rename var21 solid_liquid_gas
	rename var23 combustion_diesel

	replace country = "FRA" if country == "France"

	destring year, replace

	foreach x of varlist total nuclear hydro hydro_pump geotherm solar tidewaveocean wind totcombust other coal lr_gas nat_gas peat other_combust solid_liquid solid_gas liquid_gas solid_liquid_gas steam combustion_diesel gas_turbine combined_cycle othertype peakload capatpeak {

		rename `x' EC_`x'
		label variable EC_`x' "Net electrical capacity | total plants | `x'" 
		
	}

	sort country industry year

	save "IEA_elec_cap40x.dta", replace



*****************************************************
*													*
*	(6) Insheet and prepare IEA Fuel Use data		* 	
*													*
*****************************************************

/*
Fuel use in electricity generation (IEA)
*/

	clear

	xmluse "IEA_elec_fuel.xml", firstrow missing

	compress

	gen industry = "40x"

	destring Time, replace

	rename Anthracite FU_anthracite
	label variable FU_anthracite "Fuel Input (TJ) in EG | total plants | Anthracite"
	rename Coking_coal FU_coking_coal
	label variable FU_coking_coal "Fuel Input (TJ) in EG | total plants | Coking_coal"
	rename Other_bituminous_coal FU_other_bit_coal
	label variable FU_other_bit_coal "Fuel Input (TJ) in EG | total plants | Other_bituminous_coal"
	rename var6 FU_subbituminous_coal
	label variable FU_subbituminous_coal "Fuel Input (TJ) in EG | total plants | Sub-bituminous coal"
	rename var7 FU_lignite_brown_coal
	label variable FU_lignite_brown_coal "Fuel Input (TJ) in EG | total plants | Lignite/brown coal"
	rename Peat FU_peat
	label variable FU_peat "Fuel Input (TJ) in EG | total plants | Peat"
	rename Patent_fuel FU_patent
	label variable FU_patent "Fuel Input (TJ) in EG | total plants | Patent_fuel"
	rename Coke_oven_coke FU_coke_oven_coke
	label variable FU_coke_oven_coke "Fuel Input (TJ) in EG | total plants | Coke_oven_coke"
	rename Gas_coke FU_gas_coke
	label variable FU_gas_coke "Fuel Input (TJ) in EG | total plants | Gas_coke"
	rename Coal_tar FU_coal_tar
	label variable FU_coal_tar "Fuel Input (TJ) in EG | total plants | Coal_tar"
	rename var13 FU_bkb_peat
	label variable FU_bkb_peat "Fuel Input (TJ) in EG | total plants | BKB/peat briquettes"
	rename Gas_works_gas FU_gas_works
	label variable FU_gas_works "Fuel Input (TJ) in EG | total plants | Gas_works_gas"
	rename Coke_oven_gas FU_coke_oven_gas
	label variable FU_coke_oven_gas "Fuel Input (TJ) in EG | total plants | Coke_oven_gas"
	rename Blast_furnace_gas FU_blast_furnaces
	label variable FU_blast_furnaces "Fuel Input (TJ) in EG | total plants | Blast_furnace_gas"
	rename Oxygen_steel_furnace_gas FU_oxygen_steel
	label variable FU_oxygen_steel "Fuel Input (TJ) in EG | total plants | Oxygen_steel_furnace_gas"
	rename Crude_oil FU_crude_oil
	label variable FU_crude_oil "Fuel Input (TJ) in EG | total plants | Crude_oil"
	rename Natural_gas_liquids FU_natgas_liquids
	label variable FU_natgas_liquids "Fuel Input (TJ) in EG | total plants | Natural_gas_liquids"
	rename Refinery_gas FU_refinery_gas
	label variable FU_refinery_gas "Fuel Input (TJ) in EG | total plants | Refinery_gas"
	rename Liquefied_petroleum_gases FU_liq_petro_gas
	label variable FU_liq_petro_gas "Fuel Input (TJ) in EG | total plants | Liquefied_petroleum_gases"
	rename Naphtha FU_naphta
	label variable FU_naphta "Fuel Input (TJ) in EG | total plants | Naphtha"
	rename Kerosene_type_jet_fuel FU_kerosene_jet
	label variable FU_kerosene_jet "Fuel Input (TJ) in EG | total plants | Kerosene_type_jet_fuel"
	rename Kerosene FU_kerosene
	label variable FU_kerosene "Fuel Input (TJ) in EG | total plants | Kerosene"
	rename var25 FU_gas_diesel
	label variable FU_gas_diesel "Fuel Input (TJ) in EG | total plants | Gas/diesel"
	rename Fuel_oil FU_fuel_oil
	label variable FU_fuel_oil "Fuel Input (TJ) in EG | total plants | Fuel_oil"
	rename Bitumen FU_bitumen
	label variable FU_bitumen "Fuel Input (TJ) in EG | total plants | Bitumen"
	rename Petroleum_coke FU_petroleum_coke
	label variable FU_petroleum_coke "Fuel Input (TJ) in EG | total plants | Petroleum_coke"
	rename var29 FU_nonspec_oil
	label variable FU_nonspec_oil "Fuel Input (TJ) in EG | total plants | Non-specified oil products"
	rename Natural_gas FU_natgas
	label variable FU_natgas "Fuel Input (TJ) in EG | total plants | Natural_gas"
	rename var31 FU_ind_waste_nr
	label variable FU_ind_waste_nr "Fuel Input (TJ) in EG | total plants | Industrial waste (non-renew)"
	rename var32 FU_mun_waste_r
	label variable FU_mun_waste_r "Fuel Input (TJ) in EG | total plants | Municipal waste (renew)"
	rename var33 FU_mun_wast_nr
	label variable FU_mun_wast_nr "Fuel Input (TJ) in EG | total plants | Municipal waste (non-renew)"
	rename var34 FU_wood_waste
	label variable FU_wood_waste "Fuel Input (TJ) in EG | total plants | Wood/woodwaste/other solid wast"
	rename Landfill_gas FU_landfillgas
	label variable FU_landfillgas "Fuel Input (TJ) in EG | total plants | Landfill_gas"
	rename Sewage_sludge_gas FU_sewage_gas
	label variable FU_sewage_gas "Fuel Input (TJ) in EG | total plants | Sewage_sludge_gas"
	rename Other_biogases FU_oth_biogases
	label variable FU_oth_biogases "Fuel Input (TJ) in EG | total plants | Other_biogases"
	rename Liquid_biofuels FU_liq_biofules
	label variable FU_liq_biofules "Fuel Input (TJ) in EG | total plants | Liquid_biofuels"
	
	replace country = "FRA" if country == "France"

	rename Time year

	sort country industry year
	save "IEA_elec_fuel40x.dta", replace


	
*********************************************
*											*
*	(7) Insheet and prepare WIOD SUT data	* 	
*											*
********************************************* 
 
/*
WIOD USE tables at purchasers prices
*/

	clear

	* First country (GBR) which serves as a template
	import excel using "GBR_SUT_feb12.xls", sheet("USE_pur") firstrow

	rename A year
	rename B code
	rename C desc

	foreach v of varlist D - AO{
	   local x : variable label `v'
	   local q_`v'  =strtoname("`x'")
	   rename `v' `q_`v''
	}

	drop if year == "year"
	drop if year == ""
	drop desc
	drop FISIM

	foreach x of varlist  AtB - USE_Pur{
		rename `x' b_`x' 
	}

	reshape long b_, i(year code) j(industry) string
	rename b_ temp

	reshape wide temp, i(year industry) j(code) string
	rename temp* v*

	foreach y of varlist year v1 - vVA {

		destring `y', replace
		
	}
	replace industry = subinstr(industry,"_","",.)
	drop if industry == "USEPur"
	replace industry = "TOT" if industry == "INTC"

	gen country = "GBR"

	save "SUTS.dta", replace

	clear


	* Countries with data from feb12
	foreach z in AUS DEU USA{

		import excel using "`z'_SUT_feb12.xls", sheet("USE_pur") firstrow


		rename A year
		rename B code
		rename C desc

		foreach v of varlist D - AO{
			local x : variable label `v'
			local q_`v'  =strtoname("`x'")
			rename `v' `q_`v''
		}

		drop if year == "year"
		drop if year == ""
		drop desc
		drop FISIM
		
		foreach x of varlist  AtB - USE_Pur{
			rename `x' b_`x' 
		}

		reshape long b_, i(year code) j(industry) string
		rename b_ temp

		reshape wide temp, i(year industry) j(code) string
		rename temp* v*

		foreach y of varlist year v1 - vVA {

			destring `y', replace
		
		}
		replace industry = subinstr(industry,"_","",.)
		drop if industry == "USEPur"
		replace industry = "TOT" if industry == "INTC"
		
		gen country = "`z'"

		append using "SUTS.dta"
		
		save "SUTS.dta", replace
		
		clear
		
	}

	* Countries with data from jan12
	foreach z in  AUT BEL CZE DNK ESP FIN FRA HUN IRL ITA JPN NLD PRT SVN SWE {

	
		dis "`z'"

		import excel using "`z'_SUT_jan12.xls", sheet("USE_pur") firstrow


		rename A year
		rename B code
		rename C desc

		foreach v of varlist D - AO{
			local x : variable label `v'
			local q_`v'  =strtoname("`x'")
			rename `v' `q_`v''
		}

		drop if year == "year"
		drop if year == ""
		drop desc
		drop FISIM
		
		foreach x of varlist  AtB - USE_Pur{
			rename `x' b_`x' 
		}

		reshape long b_, i(year code) j(industry) string
		rename b_ temp

		reshape wide temp, i(year industry) j(code) string
		rename temp* v*

		foreach y of varlist year v1 - vVA {

			destring `y', replace
		
		}
		replace industry = subinstr(industry,"_","",.)
		drop if industry == "USEPur"
		replace industry = "TOT" if industry == "INTC"
		
		gen country = "`z'"

		append using "SUTS.dta"
		
		save "SUTS.dta", replace
		
		clear
		
	}

	use "SUTS.dta"

	order country industry year
	sort country industry year

	gen IIE = v10 + v11 + v12 + v23 + v40
	gen IIS = v50 +  v51 + v52 + v55 + v60 + v61 + v62 + v63 + v64 + v65 + v66 + v67 + v70 + v71 + v72 + v73 + v74 + v75 + v80 + v85 + v90 + v91 + v92 + v93 + v95
	gen IIM = v1 + v13 + v14 + v15 + v16 + v17 + v18 + v19 + v2 + v20 + v21 + v22 + v24 + v25 + v26 + v27 + v28 + v29 + v30 + v31 + v32 + v33 + v34 + v35 + v36 + v37 + v41 + v45 + v5

	keep country industry year IIE IIS IIM

	sort country industry year 
	save "SUTS.dta", replace




*********************************************************************
*																	*
*	(8) Insheet and prepare IEA Electricity Sector Prices data		* 	
*																	*
*********************************************************************

/*
Fuel prices for the electricity generating sector (IEA)
*/

	clear

	xmluse "IEA_elec_prices_NCV.xml", firstrow

	rename Country country
	destring Time, replace
	rename Time year

	compress

	rename Steam_coal p_elec_steam_c
	label variable p_elec_steam_c "Steam Coal - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Coking_coal p_elec_coking_c
	label variable p_elec_coking_c "Coking Coal - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Automotive_diesel_fuel p_elec_autom_d
	label variable p_elec_autom_d "Automotive diesel fuel - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Electricity p_elec_elec
	label variable p_elec_elec "Electricity - Total Price (US$/toe) (NCV) - Electricity sector"
	rename High_sulphur_fuel_oil p_elec_hsf_o
	label variable p_elec_hsf_o "High sulphur fuel oil - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Premium_Leaded_Gasoline p_elec_pl_gl
	label variable p_elec_pl_gl "Premium_Leaded_Gasoline - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Regular_Leaded_Gasoline p_elec_rl_gl
	label variable p_elec_rl_gl "Regular_Leaded_Gasoline - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Light_fuel_oil p_elec_lf_o
	label variable p_elec_lf_o "Light fuel oil - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Liquefied_Petroleum_Gas p_elec_lp_g
	label variable p_elec_lp_g "Liquefied_Petroleum_Gas - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Low_Sulphur_Fuel_Oil p_elec_lsf_o
	label variable p_elec_lsf_o "Low Sulphur Fuel Oil - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Natural_gas p_elec_nat_g
	label variable p_elec_nat_g "Natural Gas - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Premium_Unleaded_95_RON p_elec_95_ron
	label variable p_elec_95_ron "Premium_Unleaded_95_RON - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Premium_Unleaded_98_RON p_elec_98_ron 
	label variable p_elec_98_ron "Premium_Unleaded_98_RON - Total Price (US$/toe) (NCV) - Electricity sector"
	rename Regular_Unleaded_Gasoline p_elec_ru_gl
	label variable p_elec_ru_gl "Regular_Unleaded_Gasoline - Total Price (US$/toe) (NCV) - Electricity sector"

	foreach x of varlist  p_elec_steam_c -  p_elec_ru_gl {

		replace `x' = . if `x' == 0
		
	}

	sort country year
	save "IEA_elec_prices.dta", replace




*********************************************************************
*																	*
*	(9) Insheet and prepare IEA Industry Sector Prices data		    * 	
*																	*
*********************************************************************

/*
Fuel and electricity prices for the industry sector (IEA)
*/

	clear

	xmluse "IEA_ind_prices_NCV", firstrow

	destring Time, replace
	rename Time year

	compress

	rename Steam_coal p_ind_steam_c
	label variable p_ind_steam_c "Steam Coal - Total Price (US$/toe) (NCV) - Industry sector"
	rename Coking_coal p_ind_coking_c
	label variable p_ind_coking_c "Coking Coal - Total Price (US$/toe) (NCV) - Industry sector"
	rename Automotive_diesel_fuel p_ind_autom_d
	label variable p_ind_autom_d "Automotive diesel fuel - Total Price (US$/toe) (NCV) - Industry sector"
	rename Electricity p_ind_elec
	label variable p_ind_elec "Electricity - Total Price (US$/toe) (NCV) - Industry sector"
	rename High_sulphur_fuel_oil p_ind_hsf_o
	label variable p_ind_hsf_o "High sulphur fuel oil - Total Price (US$/toe) (NCV) - Industry sector"
	rename Premium_Leaded_Gasoline p_ind_pl_gl
	label variable p_ind_pl_gl "Premium_Leaded_Gasoline - Total Price (US$/toe) (NCV) - Industry sector"
	rename Regular_Leaded_Gasoline p_ind_rl_gl
	label variable p_ind_rl_gl "Regular_Leaded_Gasoline - Total Price (US$/toe) (NCV) - Industry sector"
	rename Light_fuel_oil p_ind_lf_o
	label variable p_ind_lf_o "Light fuel oil - Total Price (US$/toe) (NCV) - Industry sector"
	rename Liquefied_Petroleum_Gas p_ind_lp_g
	label variable p_ind_lp_g "Liquefied_Petroleum_Gas - Total Price (US$/toe) (NCV) - Industry sector"
	rename Low_Sulphur_Fuel_Oil p_ind_lsf_o
	label variable p_ind_lsf_o "Low Sulphur Fuel Oil - Total Price (US$/toe) (NCV) - Industry sector"
	rename Natural_gas p_ind_nat_g
	label variable p_ind_nat_g "Natural Gas - Total Price (US$/toe) (NCV) - Industry sector"
	rename Premium_Unleaded_95_RON p_ind_95_ron
	label variable p_ind_95_ron "Premium_Unleaded_95_RON - Total Price (US$/toe) (NCV) - Industry sector"
	rename Premium_Unleaded_98_RON p_ind_98_ron 
	label variable p_ind_98_ron "Premium_Unleaded_98_RON - Total Price (US$/toe) (NCV) - Industry sector"
	rename Regular_Unleaded_Gasoline p_ind_ru_gl
	label variable p_ind_ru_gl "Regular_Unleaded_Gasoline - Total Price (US$/toe) (NCV) - Industry sector"

	foreach x of varlist   p_ind_steam_c - p_ind_ru_gl {

		replace `x' = . if `x' == 0
		
	}

	sort country year
	save "IEA_ind_prices.dta", replace




*********************************
*								*
*	(10) Merge data sets		* 	
*								*
*********************************

/*
Merges the previously generated dta files into one database
*/

	clear
	use "SEA.dta"

	sort country industry year
	merge country industry using "PPP97.dta"
	drop _merge

	sort country industry year
	merge country industry using "EM.dta"
	drop _merge

	sort country industry year
	merge country industry year using "IEA_elec_gen40x.dta" , update
	drop _merge

	sort country industry year
	merge country industry year using "IEA_elec_cap40x.dta" , update
	drop _merge

	sort country industry year
	merge country industry year using "IEA_elec_fuel40x.dta" , update
	drop _merge

	sort country industry year
	merge country industry year using "SUTS.dta" , update
	drop _merge

	sort country year
	merge country year using "IEA_ind_prices.dta", update
	drop _merge

	sort country year
	merge country year using "IEA_elec_prices.dta", update
	drop _merge

	save "WIOD.dta", replace




*********************************************
*									        *
*	(11) Copy Information to Subsectors		* 	
*									        *
*********************************************

	clear

	use "WIOD.dta"

	
	***
	* Copy price indices of sector E to its subsector 40x
	***
	foreach x of varlist ppp_cap ppp_lab ppp_va ppp_ii ppp_so GO_P VA_P II_P GFCF_P{
		sort country industry year
		gen temp1 = `x' if industry == "E"
		bys country year: egen temp2 = min(temp1) if industry == "E" | industry == "40x" 
		replace `x' = temp2 if industry == "40x" 
		drop temp1 temp2
	}

	
	sort country industry year

	
	***
	* Copy ppp values of sector G to all of its subsectors 50, 51 and 52
	***
	foreach x of varlist ppp_cap ppp_lab ppp_va ppp_ii ppp_so{
		sort country industry year
		gen temp1 = `x' if industry == "G"
		by country: egen temp2 = min(temp1) if industry == "G" | industry == "50" | industry == "51" | industry == "52"
		replace `x' = temp2 if industry == "50" | industry == "51" | industry == "52"
		drop temp1 temp2
	}
	
	***
	* Copy ppp values of sector 17t19 to all of its subsectors 17t18 and 19
	***
	foreach x of varlist ppp_cap ppp_lab ppp_va ppp_ii ppp_so{
		sort country industry year
		gen temp1 = `x' if industry == "17t19"
		by country: egen temp2 = min(temp1) if industry == "17t19" | industry == "17t18" | industry == "19" 
		replace `x' = temp2 if industry == "17t18" | industry == "19"
		drop temp1 temp2
	}

	***
	* Copy ppp values of sector 60t63 to all of its subsectors 60, 61, 62 and 63
	***
	foreach x of varlist ppp_cap ppp_lab ppp_va ppp_ii ppp_so{
		sort country industry year
		gen temp1 = `x' if industry == "60t63"
		by country: egen temp2 = min(temp1) if industry == "60t63" | industry == "60" | industry == "61" | industry == "62" | industry == "63"
		replace `x' = temp2 if industry == "60" | industry == "61" | industry == "62" | industry == "63"
		drop temp1 temp2
	}


	save "WIOD_agg_subsectors.dta", replace




*************************************************
*												*
*	(12) Main raw data processing section		* 	
*												*
*************************************************

/*
Deflating, renaming and labeling of variables
*/

	clear

	use "WIOD_agg_subsectors.dta"

	* Generate deflators		
	rename GO_P pgo
	rename VA_P pva
	rename II_P pii
	rename GFCF_P pcf
	label variable pcf "GFCF_P | Price levels of gross fixed capital formation, 1995=100"

	* Generate variables which contain the US values of pgo pva pii pcf
	foreach z of varlist pva pii pcf{					
		gen `z'x = .
		replace `z'x = `z' if country == "USA"
		bys industry year: egen `z'_us = min(`z'x)
		drop `z'x 
	}

	sort country industry year
	by country industry: gen PPP_go = ( pgo ) / ( pgo[3] ) * ppp_so
	sort country industry year
	by country industry: gen PPP_va = ( pva ) / ( pva[3]  ) * ppp_va
	sort country industry year
	by country industry: gen PPP_ii = ( pii  ) / ( pii[3]  ) * ppp_ii
	sort country industry year
	by country industry: gen PPP_cf = ( pcf[1]) / ( pcf[3] ) * ppp_cap
	sort country industry year

	* See van der Werf and Claudia M. Buch 2008, the great risk shift
	by country industry: gen PPP_cap = ( pva ) / ( pva[3]  ) * ppp_cap
	sort country industry year
	by country industry: gen PPP_lab = ( pva ) / ( pva[3]  ) * ppp_lab
	sort country industry year

	* Deflation of socio-economic monetary variables
	rename GO go
	replace go = go * 1000000 / PPP_go
	label variable go "GO | Gross output at real 1997 US dollar (PPP)"
	rename VA va
	replace va = va * 1000000 / PPP_va
	label variable va "VA | Gross value added at real 1997 US dollar (PPP)"
	rename II xii
	replace xii = xii * 1000000 / PPP_ii
	label variable xii "II | Intermediate inputs at real 1997 US dollar (PPP)"
	rename IIE xiie
	replace xiie = xiie * 1000000 / PPP_ii
	label variable xiie "IIE | Intermediate energy inputs at real 1997 US dollar (PPP)"
	rename IIS xiis
	replace xiis = xiis * 1000000 / PPP_ii
	label variable xiis "IIS | Intermediate service inputs at real 1997 US dollar (PPP)"
	rename IIM xiim
	replace xiim = xiim * 1000000 / PPP_ii
	label variable xiim "IIM | Intermediate materials inputs at real 1997 US dollar (PPP)"
	rename H_EMP xl
	replace xl = xl * 1000000
	label variable xl "H_EMP | Total hours worked by persons engaged"
	rename K_GFCF xk
	replace xk = xk / PPP_cf
	label variable xk "K_GFCF | Real fixed capital stock, at real 1997 US dollar (PPP)"

	rename LAB cl
	replace cl = cl * 1000000 / PPP_lab
	label variable cl "LAB | Labour compensation at real 1997 US dollar (PPP)"
	rename CAP ck
	replace ck = ck * 1000000 / PPP_cap
	label variable ck "CAP | Capital compensation at real 1997 US dollar (PPP)"


	* Fuel Prices
	foreach y of varlist   p_ind_steam_c - p_elec_ru_gl {

		replace `y' = `y' / 0.041868 * pii_us[3] / pii_us							// Converting nominal US$/toe (NCV) prices into real US$ per TJ using conversion factor (toe = 0.041868 * TJ)
		
	}


	label variable p_ind_steam_c "Steam Coal - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_coking_c "Coking Coal - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_elec "Electricity - Total Price (real 1997 US$/toe) (NCV) - Industry sector"
	label variable p_ind_hsf_o "High sulphur fuel oil - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_lf_o "Light fuel oil - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_lsf_o "Low Sulphur Fuel Oil - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_nat_g "Natural Gas - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_elec_steam_c "Steam Coal - Total Price (real 1997 US$/TJ) (NCV) - Electriciy sector"
	label variable p_elec_coking_c "Coking Coal - Total Price (real 1997 US$/TJ) (NCV) - Electriciy sector"
	label variable p_elec_coking_c "Coking Coal - Total Price (real 1997 US$/TJ) (NCV) - Electriciy sector"
	label variable p_elec_hsf_o "High sulphur fuel oil - Total Price (real 1997 US$/TJ) (NCV) - Electriciy sector"
	label variable p_elec_lf_o "Light fuel oil - Total Price (real 1997 US$/TJ) (NCV) - Electriciy sector"
	label variable p_elec_lsf_o "Low Sulphur Fuel Oil - Total Price (real 1997 US$/TJ) (NCV) - Electriciy sector"
	label variable p_elec_nat_g "Natural Gas - Total Price (real 1997 US$/TJ) (NCV) - Electriciy sector"
	label variable p_ind_autom_d "Automotive diesel fuel - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_pl_gl "Premium_Leaded_Gasoline - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_rl_gl "Regular_Leaded_Gasoline - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_lp_g "Liquefied_Petroleum_Gas - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_95_ron "Premium_Unleaded_95_RON - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_98_ron "Premium_Unleaded_98_RON - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_ind_ru_gl "Regular_Unleaded_Gasoline - Total Price (real 1997 US$/TJ) (NCV) - Industry sector"
	label variable p_elec_autom_d "Automotive diesel fuel - Total Price (real 1997 US$/TJ) (NCV) - Electricty sector"
	label variable p_elec_pl_gl "Premium_Leaded_Gasoline - Total Price (real 1997 US$/TJ) (NCV) - Electricty sector"
	label variable p_elec_rl_gl "Regular_Leaded_Gasoline - Total Price (real 1997 US$/TJ) (NCV) - Electricty sector"
	label variable p_elec_lp_g "Liquefied_Petroleum_Gas - Total Price (real 1997 US$/TJ) (NCV) - Electricty sector"
	label variable p_elec_95_ron "Premium_Unleaded_95_RON - Total Price (real 1997 US$/TJ) (NCV) - Electricty sector"
	label variable p_elec_98_ron "Premium_Unleaded_98_RON - Total Price (real 1997 US$/TJ) (NCV) - Electricty sector"
	label variable p_elec_ru_gl "Regular_Unleaded_Gasoline - Total Price (real 1997 US$/TJ) (NCV) - Electricty sector"


	* Drop p_ind values for the electricity sector
	foreach y of varlist   p_ind_steam_c - p_ind_ru_gl {

		replace `y' = . if industry == "E" | industry == "40x" 
			
	} 



	***
	* GENERATION OF CLEAN AND DIRTY AGGREGATES
	***

	* Electricity generation capacity
	gen EC_c = EC_nuclear + EC_hydro + EC_geotherm +  EC_solar + EC_tidewaveocean + EC_wind
	gen EC_d = EC_totcombust + EC_other
	label variable EC_c "Net installed clean generation capacity (MW)"
	label variable EC_d "Net installed dirty generation capacity (MW)"

	* Fuel use in electricity generation
	gen FU_c =  FU_ind_waste_nr + FU_mun_waste_r + FU_mun_wast_nr + FU_wood_waste + FU_landfillgas + FU_sewage_gas + FU_oth_biogases + FU_liq_biofules
	gen FU_d =  FU_anthracite + FU_coking_coal + FU_other_bit_coal + FU_subbituminous_coal + FU_lignite_brown_coal + FU_peat + ///
	 FU_patent + FU_coke_oven_coke + FU_gas_coke + FU_coal_tar + FU_bkb_peat + FU_gas_works + FU_coke_oven_gas + FU_blast_furnaces + FU_oxygen_steel + FU_crude_oil + FU_natgas_liquids + FU_refinery_gas + FU_liq_petro_gas + ///
	 FU_naphta + FU_kerosene_jet + FU_kerosene + FU_gas_diesel + FU_fuel_oil + FU_bitumen + FU_petroleum_coke + FU_nonspec_oil + FU_natgas
	gen FU_total = FU_c + FU_d
	label variable FU_c "Fuel input in clean electricity generating technologies (in TJ)"
	label variable FU_d "Fuel input in dirty electricity generating technologies (in TJ)"
	label variable FU_total "Fuel input in electricity generating technologies (in TJ)"
	
	* Clean and dirty capital stocks based on installed capacity values and given investment costs (EIA overnight capital costs (current $/kW))
	gen EC_c_alt = (EC_nuclear * 1000 * 1729 + EC_hydro * 1000 * 1319 + EC_geotherm * 1000 * 1626 + EC_solar * 1000 * 3037 + EC_tidewaveocean * 5000 * 1000 + EC_wind * 919 * 1000) / ( pcf_us[217]) * ( pcf_us[214] ) if year <= 2000 & industry == "40x"
	replace EC_c_alt = (EC_nuclear * 1000 * 1772 + EC_hydro * 1000 * 1319 + EC_geotherm * 1000 * 1663 + EC_solar * 1000 * 2737 + EC_tidewaveocean * 5000 * 1000 + EC_wind * 918 * 1000) / ( pcf_us[218]) * ( pcf_us[214] ) if year == 2001 & industry == "40x"
	replace EC_c_alt = (EC_nuclear * 1000 * 1750 + EC_hydro * 1000 * 1319 + EC_geotherm * 1000 * 1681 + EC_solar * 1000 * 2797 + EC_tidewaveocean * 5000 * 1000+ EC_wind * 938 * 1000) / ( pcf_us[219]) * ( pcf_us[214] ) if year == 2002 & industry == "40x"
	replace EC_c_alt = (EC_nuclear * 1000 * 1669 + EC_hydro * 1000 * 1319 + EC_geotherm * 1000 * 2099 + EC_solar * 1000 * 3144 + EC_tidewaveocean * 5000 * 1000+ EC_wind * 949 * 1000) / ( pcf_us[220]) * ( pcf_us[214] ) if year == 2003 & industry == "40x"
	replace EC_c_alt = (EC_nuclear * 1000 * 1694 + EC_hydro * 1000 * 1319 + EC_geotherm * 1000 * 2960 + EC_solar * 1000 * 3191 + EC_tidewaveocean * 5000 * 1000+ EC_wind * 1060 *1000 ) / ( pcf_us[221]) * ( pcf_us[214] ) if year == 2004 & industry == "40x"
	replace EC_c_alt = (EC_nuclear * 1000 * 1744 + EC_hydro * 1000 * 1320 + EC_geotherm * 1000 * 2100 + EC_solar * 1000 * 3285 + EC_tidewaveocean * 5000 * 1000+ EC_wind * 1091 *1000 ) / ( pcf_us[222]) * ( pcf_us[214] ) if year == 2005 & industry == "40x"
	replace EC_c_alt = (EC_nuclear * 1000 * 1802 + EC_hydro * 1000 * 1364 + EC_geotherm * 1000 * 1790 + EC_solar * 1000 * 3394 + EC_tidewaveocean * 5000 * 1000+ EC_wind * 1127 *1000 ) / ( pcf_us[223]) * ( pcf_us[214] ) if year == 2006 & industry == "40x"
	replace EC_c_alt = (EC_nuclear * 1000 * 2143 + EC_hydro * 1000 * 1410 + EC_geotherm * 1000 * 1057 + EC_solar * 1000 * 4440 + EC_tidewaveocean * 5000 * 1000+ EC_wind * 1340 *1000 ) / ( pcf_us[224]) * ( pcf_us[214] ) if year == 2007 & industry == "40x"
	label var EC_c_alt "Alternative Capital Proxy: Capital stock associated with clean technologies (EIA based)"
	gen EC_d_alt = (EC_d * 659 *1000 ) / ( pcf_us[217]) * ( pcf_us[214] ) if year <= 2000 & industry == "40x"
	replace EC_d_alt = (EC_d * 675 *1000 ) / ( pcf_us[218]) * ( pcf_us[214] ) if year == 2001 & industry == "40x"
	replace EC_d_alt = (EC_d * 709 *1000 ) / ( pcf_us[219]) * ( pcf_us[214] ) if year == 2002 & industry == "40x"
	replace EC_d_alt = (EC_d * 717 *1000 ) / ( pcf_us[220]) * ( pcf_us[214] ) if year == 2003 & industry == "40x"
	replace EC_d_alt = (EC_d * 705 *1000 ) / ( pcf_us[221]) * ( pcf_us[214] ) if year == 2004 & industry == "40x"
	replace EC_d_alt = (EC_d * 726 * 1000) / ( pcf_us[222]) * ( pcf_us[214] ) if year == 2005 & industry == "40x"
	replace EC_d_alt = (EC_d * 750 *1000 ) / ( pcf_us[223]) * ( pcf_us[214] ) if year == 2006 & industry == "40x"
	replace EC_d_alt = (EC_d * 829 *1000 ) / ( pcf_us[224]) * ( pcf_us[214] ) if year == 2007 & industry == "40x"
	label var EC_d_alt "Alternative Capital Proxy: Capital stock associated with dirty technologies (EIA based)"

	
	
	***
	* Dropping and Ordering of variables
	***
	
	drop  COMP EMP EMPE GFCF pgo pii GO_QI H_EMPE H_HS H_LS H_MS II_QI LABHS LABLS LABMS pva VA_QI ppp_cap ppp_lab ppp_va ppp_ii ppp_so PPP_go PPP_va PPP_ii PPP_cf PPP_cap PPP_lab pva_us pii_us pcf_us
	drop if industry == "17t19" | industry == "60t63" | industry == "G"  		// Aggregates not needed anymore
	
	/*
	* Generate sector dummies 
	gen sector = "."
	replace sector = "agr" if industry == "AtB"
	replace sector = "min" if industry == "C"
	replace sector = "manu" if industry == "15t16" | industry == "17t18" | industry == "19" | industry == "20" | industry == "21t22" | industry == "23" | industry == "24" | industry == "25" | industry == "26" | industry == "27t28" ///
	 | industry == "29" | industry == "30t33" | industry == "34t35" | industry == "36t37"
	replace sector = "constru" if industry == "F"
	replace sector = "serv" if industry == "50" | industry == "51" | industry == "52" | industry == "H"  | industry == "64" | industry == "J" | industry == "70" ///
	 | industry == "71t74" | industry == "L" | industry == "M" | industry == "N" | industry == "O"
	replace sector = "transp" if industry == "60" | industry == "61" | industry == "62" | industry == "63"
	*/
	
	encode country, gen(cntry)
	drop country
	rename cntry country

	encode industry, gen(intry)
	drop industry
	rename intry industry

	*encode sector, gen(sctor)
	*drop sector
	*rename sctor sector

	*order country industry sector year
	order country industry year

	* Save data set which combines electricity sector and non-energy industries data
	save "WIOD_cleaned.dta", replace	

	
	


*************************************************************************
*																		*
*	(13) Split data into electricity and non-engergy sector data		* 	
*																		*
*************************************************************************

/*
Split data set into two data sets and generate variables necessary for estimation
*/

	
	***
	* Electricity sector data
	***

	clear

	use "WIOD_cleaned.dta"
	
	* Generate reg variables
	gen ln_eg = ln(EG_total)
	gen ln_ecc = ln(EC_c)				
	gen ln_ecd = ln(EC_d)
	gen ln_egecd = ln_eg - ln_ecd
	gen ln_eccd = ln_ecc-ln_ecd
	gen ln_eccd_2 = 0.5*(ln_ecc-ln_ecd)^2
	
	* Generate alternative capital proxy
	gen ln_ecc_alt = ln(EC_c_alt)
	gen ln_ecd_alt = ln(EC_d_alt)

	* Define sample
	keep if industry == 15															// keep only industry 40x (the electricity sector)
	drop if country == 13															// EC_total is strange
	drop if country == 31 															// has only 4 observations in most regressions
	
	* Drop variables not needed anymore
	drop EM_hcoal - EM_total
	drop EG_nuclear - EG_totcombust
	drop EC_nuclear - EC_capatpeak
	drop FU_anthracite - FU_liq_biofules
	drop p_ind_steam_c - p_elec_ru_gl
	drop ck pcf go xl xii xk cl va xiie xiis xiim
	*drop sector
	
	* Identify estimation sample
	qui reg ln_eg EC_c EC_d EC_total  FU_d 										// generates one sample which can be used for all subsequent estimations
	keep if e(sample)

	label variable country "26 countries"
	label variable industry "1 industry (NACE 1.1)"
	label variable year "1995 - 2009"	
	
	compress
	
	save "electricity_sector.dta", replace

	
	
	***
	* Non-energy industries data
	***
	
	clear

	use "WIOD_cleaned.dta"

	* Drop observations not used in estimation 
	keep if industry != 15 & industry != 28 & industry != 38 																						// the total economy aggregate and all energy industries (TOT, E, 40x)	
	drop if industry == 4 | industry == 6 | industry == 20 | industry == 21 | industry == 24														// outlier in terms of energy use (NACE 20, 23, 61,62 70)
	keep if year <= 2007


	* Replace missings by zero
	foreach a of varlist EM_hcoal - EM_total{

		replace `a' = 0 if `a' == . 			

	}

	* Emission relevant energy use
	gen xc = (EM_biogasol + EM_biodiesel + EM_biogas + EM_othrenew + EM_electr + EM_heatprod + EM_hydro + EM_geotherm + EM_solar + EM_wind + EM_othsourc + EM_nuclear + EM_waste)
	gen xd = (EM_hcoal + EM_bcoal + EM_coke + EM_crude + EM_diesel + EM_gasoline + EM_jetfuel + EM_lfo + EM_hfo + EM_naphta + EM_othpetro + EM_natgas + EM_othgas + EM_loss)
	rename EM_total xe
	label var xc "Emission relevant energy use of clean sources (TJ)"
	label var xd "Emission relevant energy use of  dirty sources (TJ)"

	* Cost of clean energy
	gen cc = p_ind_elec * xc

	* Price of dirty energy
	gen pcoal = .
	replace pcoal = p_ind_steam_c																		
	replace pcoal = p_elec_steam_c if p_ind_steam_c ==. 												
	replace pcoal = p_ind_coking_c if p_ind_steam_c ==. & p_elec_steam_c ==.
	replace pcoal = p_elec_coking_c if p_ind_steam_c ==. & p_elec_steam_c ==. & p_ind_coking_c ==.
	gen poil = .
	replace poil = p_ind_hsf_o 												
	replace poil = p_elec_hsf_o if p_ind_hsf_o ==.
	replace poil = p_ind_lf_o if p_elec_hsf_o ==. & p_ind_hsf_o ==.
	replace poil = p_elec_lf_o if p_ind_lf_o ==. & p_elec_hsf_o ==. & p_ind_hsf_o ==.
	replace poil = p_ind_lsf_o if p_elec_lf_o ==. & p_ind_lf_o ==. & p_elec_hsf_o ==. & p_ind_hsf_o ==.
	replace poil = p_elec_lsf_o if p_ind_lsf_o ==. & p_elec_lf_o ==. & p_ind_lf_o ==. & p_elec_hsf_o ==. & p_ind_hsf_o ==.
	gen pngas = .
	replace pngas = p_ind_nat_g 													
	replace pngas = p_elec_nat_g if p_ind_nat_g ==.
	gen pgl = p_ind_autom_d

	* Cost of dirty energy
	gen cd = (EM_hcoal + EM_bcoal + EM_coke) * pcoal  + (EM_diesel + EM_gasoline + EM_jetfuel + EM_othpetro + EM_naphta) * pgl + (EM_lfo + EM_hfo + EM_crude) * poil + (EM_natgas + EM_othgas ) * pngas

	* Total cost of all inputs
	gen ctot = ck + cl + cc + cd

	* Prices of inputs
	gen pk = ck / xk
	gen pl = cl / xl
	gen pc = cc / xc
	gen pd = cd / xd
	label var pk "Price of capital"
	label var pl "Price of hours worked"
	label var pc "Price of energy use of clean sources"
	label var pd "Price of energy use of dirty sources"

	* Cost shares of inputs
	gen sk = ck / ctot
	gen sl = cl / ctot
	gen sc = cc / ctot
	gen sd = cd / ctot
	label var sk "Cost share of capital"
	label var sl "Cost share of hours worked"
	label var sc "Cost share of energy use of clean sources"
	label var sd "Cost share of energy use of dirty sources"
	
	* Output (either GO or va+intermediates)
	gen xiims = xiis + xiim
	gen vaxiie = va + xiie
	label var xiims "IIS + IIM | Intermediate service and material inputs at real 1997 US dollar (PPP)"
	label var vaxiie "VA + IIE | Gross value added plus intermediate energy inputs at real 1997 US dollar (PPP)"
	
	* Generate translog- / log-values
	local QUANT go va xk xl xc xd xe xii xiie xiis xiim	xiims vaxiie					
	local PRICE pk pl pc pd																					

	foreach z of local QUANT {
		gen ln_`z' = ln(`z')
		gen ln_`z'_2 = 0.5 * ln(`z') * ln(`z')
	}

	local QUANTL go va xk xc xd xe xii xiie xiis xiim xiims vaxiie
	foreach z of local QUANTL {
		gen `z'l=`z'/xl
		gen ln_`z'l=ln(`z'l)
	}

	foreach z of local PRICE {
		gen ln_`z' = ln(`z')
		gen ln_`z'_2 = 0.5 * ln(`z') * ln(`z')
	}

		gen ln_ctot = ln(ctot)
		gen ln_xdc=ln_xd-ln_xc
		gen ln_xdc_2=0.5*ln_xdc^2
		
	* Variables for price-based estimates
	gen sde=pd*xd/(pc*xc+pd*xd)
	gen ln_sde=ln(sde)
	gen sce=pc*xc/(pc*xc+pd*xd)
	gen ln_sce=ln(sce)
	gen pe=(pc*xc+pd*xd)/(xc+xd)
	gen ln_pe=ln(pe)
	gen ln_pdpe=ln_pd-ln_pe
	gen ln_pcpe=ln_pc-ln_pe
	
	* Drop variables not needed anymore
	drop EM_hcoal - EM_loss
	drop EG_nuclear - EG_totcombust
	drop EC_nuclear - EC_capatpeak
	drop FU_anthracite - FU_liq_biofules
	drop p_ind_steam_c - p_elec_ru_gl
	drop ck pcf cl EG_total EC_total EC_c EC_d FU_c FU_d FU_total EC_c_alt EC_d_alt cc pcoal poil pngas pgl cd ctot
	
	* Identify estimation sample
	keep if go!=. & xl !=. & xk!=. & xii!=. & xe !=. & xc!=. & xd!=. & va!=.		 		// drop observations with missings
	drop if xc == 0 | xd == 0																						

	label variable country "19 countries"
	label variable industry "28 industries (NACE 1.1)"
	*label variable sector "6 sectors"
	label variable year "1995 - 2007"

	compress
	
	save "nonenergy_industries.dta", replace




*************************************
*									*
*	(14) Delete temporary files		*
*									*
*************************************

/*
Delete files not needed anymore
*/

	erase "WIOD_cleaned.dta"
	erase "WIOD_agg_subsectors.dta"
	erase "WIOD.dta"
	erase "SUTS.dta"
	erase "IEA_ind_prices.dta"
	erase "IEA_elec_prices.dta"
	erase "IEA_elec_fuel40x.dta"
	erase "IEA_elec_gen40x.dta"
	erase "IEA_elec_cap40x.dta"
	erase "EM.dta"
	erase "PPP97.dta"
	erase "SEA.dta"



clear all



